package com.lei.chinese_herbal_medicine.Util;

import com.lei.chinese_herbal_medicine.Entity.TestDataset;
import com.lei.chinese_herbal_medicine.Entity.TrainDataset;
import com.lei.chinese_herbal_medicine.Mapper.TestDatasetMapper;
import com.lei.chinese_herbal_medicine.Mapper.TrainDatasetMapper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

public class DatasetOutputUtil {
    /***
     * 将数据库中的训练集同步至目标训练集文件夹中
     * @param trainDatasetDirPath 训练集文件夹路径
     * @param trainDatasets 指定中草药的数据集
     * @param trainDatasetMapper 数据集图片查询
     * @throws IOException
     */
    public static void TrainDatasetOutput(String trainDatasetDirPath, List<TrainDataset> trainDatasets, TrainDatasetMapper trainDatasetMapper) throws IOException {
        File file=new File(trainDatasetDirPath);
        if(!file.exists()){
            file.mkdirs();
        }
        String[] files=file.list();

        if(trainDatasets!=null&&trainDatasets.size()>=1){
            String herbName=trainDatasets.get(0).getTrainDatasetName();
            String trainDatasetLableDirPath=trainDatasetDirPath+"\\"+herbName;
            File train_lable_dir=new File(trainDatasetLableDirPath);
            if(!train_lable_dir.exists()){
                train_lable_dir.mkdirs();
            }
            String trainDatasetImgPath="";
            File ImgFile=null;
            OutputStream outputStream=null;
            for (TrainDataset trainDataset : trainDatasets) {
                trainDatasetImgPath=trainDatasetLableDirPath+"\\"+trainDataset.getId()+".jpg";
                ImgFile=new File(trainDatasetImgPath);
                if(!ImgFile.exists()){
                    TrainDataset datasetWithImg=trainDatasetMapper.selectByPrimaryKeyWithBLOBs(trainDataset.getId());
                    outputStream=new FileOutputStream(ImgFile);
                    outputStream.write(datasetWithImg.getImg());
                    outputStream.flush();
                    outputStream.close();
                }
            }
        }
    }

    public static void TestDatasetOutput(String testDatasetDirPath, List<TestDataset> testDatasets, TestDatasetMapper testDatasetMapper) throws IOException {
        File file=new File(testDatasetDirPath);
        if(!file.exists()){
            file.mkdirs();
        }
        String[] files=file.list();

        if(testDatasets!=null&&testDatasets.size()>=1){
            String herbName=testDatasets.get(0).getTestDatasetName();
            String testDatasetLableDirPath=testDatasetDirPath+"\\"+herbName;
            File test_lable_dir=new File(testDatasetLableDirPath);
            if(!test_lable_dir.exists()){
                test_lable_dir.mkdirs();
            }
            String testDatasetImgPath="";
            File ImgFile=null;
            OutputStream outputStream=null;
            for (TestDataset testDataset : testDatasets) {
                testDatasetImgPath=testDatasetLableDirPath+"\\"+testDataset.getId()+".jpg";
                ImgFile=new File(testDatasetImgPath);
                if(!ImgFile.exists()){
                    TestDataset datasetWithImg=testDatasetMapper.selectByPrimaryKeyWithBLOBs(testDataset.getId());
                    outputStream=new FileOutputStream(ImgFile);
                    outputStream.write(datasetWithImg.getImg());
                    outputStream.flush();
                    outputStream.close();
                }
            }
        }
    }
}
